package leetcode.p114;

public class Foo {
    private int step = 1;

    public Foo(){}

    public void first(Runnable printFirst){
        while (true){
            synchronized (this){
                if (step == 1){
                    break;
                }
            }
            Thread.yield();
        }

        printFirst.run();

        synchronized (this) {
            step = 2;
        }
    }
    public void second(Runnable printSecond){
        while (true){
            synchronized (this){
                if (step == 1){
                    break;
                }
            }
            Thread.yield();
        }
        printSecond.run();
        synchronized (this) {
            step = 3;
        }
    }
    public void third(Runnable printThird){
        while (true){
            synchronized (this){
                if (step == 1){
                    break;
                }
            }
            Thread.yield();
        }
        printThird.run();
        synchronized (this) {
            step = 2;
        }
    }
}
